package com.hiray.taglayoutmanager;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hiray on 2017/12/25.
 *
 * @author hiray
 */

public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.ViewHolder> {
    private static final String TAG = "LinearAdapter";
    private LayoutInflater layoutInflater;
    private Context context;

    private List<String> datas = new ArrayList<>();

    public void setDatas(List<String> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public LinearAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i(TAG, "onCreateViewHolder: ");
        if (layoutInflater == null) {
            context = parent.getContext();
            layoutInflater = LayoutInflater.from(parent.getContext());
        }
        int layoutId = context.getResources().getIdentifier("bar0", "layout", context.getPackageName());
        return new LinearAdapter.ViewHolder(layoutInflater.inflate(layoutId, parent, false));
    }

//    @Override
//    public int getItemViewType(int position) {
//        return position % 2;
//    }

    @Override
    public void onBindViewHolder(LinearAdapter.ViewHolder holder, int position) {
        String text = datas.get(holder.getAdapterPosition());
        holder.text.setText(text);
        holder.itemView.setTag(holder.getAdapterPosition());
        holder.itemView.setBackgroundResource(holder.getAdapterPosition() % 2 == 0 ? R.color.colorAccent : R.color.colorPrimary);
        Log.i(TAG, "onBindViewHolder: ----"+position);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {


        TextView text;

        ViewHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.onClick(getAdapterPosition());
                }
            });
        }
    }

    CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onClick(int pos);
    }
}
